package cn.gcjxsc.repository.specs;

import cn.gcjxsc.common.consts.OrderDiscr;
import cn.gcjxsc.common.consts.OrderStatus;
import cn.gcjxsc.common.consts.PayStatus;
import cn.gcjxsc.entity.mall.BaseOrder;
import cn.gcjxsc.entity.mall.BaseOrder_;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pumbaa
 */
public class OrderSpec {

    /**
     * 待自动完成订单查询条件
     * 1.商城购物订单
     * 2.已支付状态
     * 3.订单为已发货状态
     * @param days 天数
     * @return
     */
    public static Specification<BaseOrder> sending2doneSpec(final int days) {
        return (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(cb.equal(root.get(BaseOrder_.discr), OrderDiscr.SHOPPING_ORDER));

            long times = System.currentTimeMillis()/1000 - 3600*24*days;

            predicateList.add(cb.equal(root.get(BaseOrder_.payStatus), PayStatus.PAID));
            predicateList.add(cb.equal(root.get(BaseOrder_.status), OrderStatus.STATUS_SHIPPED));
            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
        };
    }
}
